package cn.wgx.modules.sys.web;

import cn.wgx.commons.annotation.ApiPermission;
import cn.wgx.modules.base.web.BaseController;
import cn.wgx.modules.sys.entity.SysNotice;
import cn.wgx.modules.sys.services.SysNoticeService;
import cn.wgx.modules.sys.util.UserUtil;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "公告通知")
//@RestController
//@RequestMapping(value = "${allPathPre}sysnotice")
public class SysNoticeController extends BaseController {
    @Resource
    SysNoticeService sysNoticeService;

    @ApiOperation(value = "给院校发布公告", notes = "当schoolId为0时所有学校人员都可以看到")
    @RequestMapping(path = "insertbatch", method = RequestMethod.POST)
    @ApiPermission
    public Map<String, Object> insertBatch(@ApiParam("学校id")  List<Integer> ids,
                                           @ApiParam("公告内容") @RequestParam String content,
                                           @ApiParam("公告标题") @RequestParam String title,
                                           @ApiParam("显示类型：0正常，1置顶") @RequestParam Integer type) {
        if (UserUtil.getUser() == null) {
            return returnErrorMap("请登录后再进行操作！");
        }
        return returnData(sysNoticeService.insertBatch(ids, content, title, type));
    }

    @ApiOperation(value = "给班级发布公告", notes = "当squadId为0时所有班级人员都可以看到")
    @RequestMapping(path = "insertsquad", method = RequestMethod.POST)
    @ApiPermission
    public Map<String, Object> insertSquad(@ApiParam("班级id") List<Integer> ids,
                                           @ApiParam("公告内容") @RequestParam String content,
                                           @ApiParam("公告标题") @RequestParam String title,
                                           @ApiParam("显示类型：0正常，1置顶") @RequestParam Integer type) {
        if (UserUtil.getUser() == null) {
            return returnErrorMap("请登录后再进行操作！");
        }
        return returnData(sysNoticeService.insertSquad(ids, content, title, type));
    }

    @ApiOperation(value = "获取公告列表(根据所在的学校、班级获取)", notes = "title、content支持模糊查询；按照置顶、时间倒序排列")
    @RequestMapping(path = "getAll", method = RequestMethod.GET)
    @ApiPermission
    public Map<String, Object> getAll(@ModelAttribute SysNotice sysNotice,
                                      @RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        if (UserUtil.getUser() == null) {
            return returnErrorMap("请登录后再进行操作！");
        }
//        sysNotice.setSchoolId(UserUtil.getUser().getSchool_id());
//        sysNotice.setSquadId(UserUtil.getUser().getSquad_id());
        return returnData(PageHelper.startPage(page, pageSize), sysNoticeService.getList(sysNotice));
    }

    @ApiOperation(value = "设置置顶")
    @RequestMapping(path = "updatestatus", method = RequestMethod.PATCH)
    @ApiPermission
    public Map<String, Object> updateStatus(@RequestParam Integer notice_id) {
        if (UserUtil.getUser() == null) {
            return returnErrorMap("请登录后再进行操作！");
        }
        return returnData(sysNoticeService.updateStatus(notice_id));
    }

    @ApiOperation(value = "逻辑删除公告(单个、批量)")
    @RequestMapping(path = "deletebatch", method = RequestMethod.DELETE)
    @ApiPermission
    public Map<String, Object> deleteBatch(@RequestParam List<Integer> ids) {
        return returnData(sysNoticeService.deleteBatch(ids, true));
    }

    @ApiOperation(value = "彻底删除公告(单个、批量)")
    @RequestMapping(path = "delete", method = RequestMethod.DELETE)
    @ApiPermission
    public Map<String, Object> delete(@RequestParam List<Integer> ids) {
        return returnData(sysNoticeService.deleteBatch(ids, false));
    }

    @ApiOperation(value = "获取单个公告")
    @RequestMapping(path = "getone", method = RequestMethod.GET)
    @ApiPermission
    public Map<String, Object> getOne(@RequestParam Integer id) {
        return returnData(sysNoticeService.getOne(id));
    }
}
